home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / BT_BLOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  3.1 KB  |  98 lines

  1. /*  bt_block.c - block level stuff */
  2. #include   "stdio.h"
  3. #include   "btree.h"
  4. #include   "bt_macro.h"
  5.  
  6. extern     IX_DESC    *pci ;        /* refers to index descriptor */
  7.                     /* for current function call  */
  8. BLOCK    *retrieve_block() ;
  9.  
  10. int  find_block(pe,pb,poff,comp_fun)    /* look for a key in a block */
  11.   ENTRY *pe ;                /* contains the target key   */
  12.   BLOCK *pb ;                /* look in this block */
  13.   int    *poff ;             /* store offset where we stop */
  14.   int    (*comp_fun) () ;        /* address of compare function */
  15.   {                    /* returns the compare results */
  16.      int   i ;                /* offset */
  17.      int   ret ;            /* result of last comparison */
  18.      ENTRY *p ;
  19.  
  20.      i = 0 ;
  21.      while( i < pb->bend )        /* repeat until end of block */
  22.     {  p = ENT_ADR(pb,i) ;        /* get entry address */
  23.                     /* compare to target key */
  24.        ret = call(comp_fun)( pe , ENT_ADR(pb,i) ) ;
  25.        if( ret <= 0 )        /* quit when the target is  */
  26.           break ;            /* <= the current entry  */
  27.        i = next_entry(pb,i) ;    /* move to next entry */
  28.     }
  29.      *poff = i ;            /* store offset where we stopped */
  30.      return ret  ;            /* result of last compare */
  31.   }
  32.  
  33.  
  34. int  ins_block(pb,pe,off)        /* add an entry to block */
  35.   BLOCK *pb ;                /* the block */
  36.   ENTRY *pe ;                /* the entry to insert */
  37.   int    off ;                /* the offset where we insert it */
  38.   {
  39.      int   ne ;
  40.  
  41.      ne = ENT_SIZE(pe) ;        /* how big is the new insert ? */
  42.                     /* move everything to end of block */
  43.      moveup( pb,off,ne) ;        /* make room for new entry  */
  44.      copy_entry(ENT_ADR(pb,off),pe) ;    /* move it in */
  45.      pb->bend = pb->bend + ne ;     /* adjust block size */
  46.   }
  47.  
  48. int  del_block(pb,off)            /* remove an entry from a block */
  49.   BLOCK *pb ;                /* the block to work on */
  50.   int    off ;                /* where to remove the entry */
  51.   {
  52.      int   ne ;
  53.  
  54.      ne = _SIZE( ENT_ADR(pb,off) ) ;    /* get entry size */
  55.      movedown(pb,off,ne) ;        /* move entries above curr. one down */
  56.      pb->bend = pb->bend - ne ;     /* adjust number of bytes */
  57.   }
  58.  
  59.  
  60. int  moveup(pb,off,n)            /* move parts of a block upward */
  61.   BLOCK *pb ;                /* the block */
  62.   int    off ;                /* place to start moveing */
  63.   int    n ;                /* how far to move things */
  64.   {
  65.      ENTRY *p ;
  66.                     /* move entries  */
  67.      mover(ENT_ADR(pb,off+n),        /* to here */
  68.        ENT_ADR(pb,off)  ,        /* from here */
  69.        pb->bend - off)  ;        /* rest of block */
  70.   }
  71.  
  72.  
  73. int  movedown(pb,off,n)         /* move parts of a block downward */
  74.   BLOCK *pb ;                /* the block */
  75.   int    off ;                /* place to start moveing */
  76.   int    n ;                /* how far down to move things */
  77.   {
  78.      ENTRY *p ;
  79.  
  80.                     /* move entries */
  81.      mover(ENT_ADR(pb,off)   ,        /* to here */
  82.        ENT_ADR(pb,off+n) ,        /* from here */
  83.        pb->bend - (off+n)) ;    /* rest of the block */
  84.   }
  85.  
  86. int  combine(pl,pr)            /* combine two blocks */
  87.   BLOCK *pl ;                /* address of left block */
  88.   BLOCK *pr ;                /* address of right block */
  89.   {
  90.      moveup(pr,0,pl->bend) ;        /* make room for left block */
  91.                     /* move in left block contents */
  92.      mover(ENT_ADR(pr,0),ENT_ADR(pl,0),pl->bend) ;
  93.      pr->bend = pr->bend + pl->bend ;    /* adjust block size */
  94.   }
  95.  
  96.  
  97.  
  98.